package cz.byteworks.android.myway.adapter;

import java.text.SimpleDateFormat;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import cz.byteworks.android.myway.activities.R;
import cz.byteworks.android.myway.db.DataHelper;
import cz.byteworks.android.myway.location.PositionService;
import cz.byteworks.android.myway.pojo.Position;

public class PositionArrayAdapter<T> extends ArrayAdapter<T> {

	List<T> list;
	Context ctx;
	LayoutInflater infl;
	DataHelper dh;
	int actualWayId;
	
	public PositionArrayAdapter(Context context, int resource, List<T> list, LayoutInflater layoutInflater) {
		super(context, resource, list);
		this.list = list;
		ctx = context;
		this.infl = layoutInflater;
		dh = new DataHelper(ctx);
		actualWayId = dh.getActualWay();
	}
	
	
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LinearLayout ll = (LinearLayout) infl.inflate(R.layout.position_item, parent, false);
		Position pos = (Position) list.get(position);
		ImageView image = (ImageView) ll.findViewById(R.id.positionImage);
		TextView note = (TextView) ll.findViewById(R.id.positionNote);
		if (pos.getMark() == 1) {
			image.setImageDrawable(ctx.getResources().getDrawable(R.drawable.map_pin_gps));
		}
		else if (pos.getMark() == 2) {
			image.setImageDrawable(ctx.getResources().getDrawable(R.drawable.map_pin_net));
		}
		else {
			image.setImageDrawable(ctx.getResources().getDrawable(R.drawable.map_pin));
		}
		TableLayout tbl = (TableLayout) ll.findViewById(R.id.positionTable);
		TableRow tr1 = (TableRow) tbl.findViewById(R.id.positionTableRow1);
		TableRow tr2 = (TableRow) tbl.findViewById(R.id.positionTableRow2);
		TableRow tr3 = (TableRow) tbl.findViewById(R.id.positionTableRow3);
		TextView tv1 = (TextView) tr1.findViewById(R.id.positionText1);
		TextView tv2 = (TextView) tr2.findViewById(R.id.positionText2);
		TextView tv3 = (TextView) tr3.findViewById(R.id.positionText3);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
		tv1.setText(sdf.format(pos.getLocateTime()));
		tv2.setText(PositionService.formatLat(pos.getLatitude())+" "+PositionService.formatLon(pos.getLongitude()));
		tv3.setText(pos.getLocality());
		note.setText(pos.getNote());
		return ll;
	}
	
	

}
